﻿using core6_website.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace z2.Controllers
{
    public class HomeController : Controller
    {

        private readonly ILogger<HomeController> _logger;
        private readonly IStudentRepo _studentRepo;

        public HomeController(ILogger<HomeController> logger, IStudentRepo studentRepo)
        {
            _logger = logger;
            _studentRepo = studentRepo;
        }

        public IActionResult Index()
        {
            var model = _studentRepo.GetAllStudents(); 
            return View(model);
        }

        public IActionResult Privacy()
        {
            return View();
        }

        public ViewResult Details(int id) {
            HomeDetailsViewModel viewModel = new HomeDetailsViewModel()
            {
                Student = _studentRepo.GetStudent(id),
                PageTitle = "学生详情"
            };

            return View(viewModel);
        }

        [HttpGet]
        public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Create(Student student)
        {
            if (ModelState.IsValid) {
                Student newStudent = _studentRepo.Add(student);
                return RedirectToAction("Details", new { id = newStudent.Id });
            }

            return View();
        }
    }
}
